package frontend.pages.renamefiles;

import net.sf.json.JSONObject;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.util.value.IValueMap;
import org.apache.wicket.util.value.ValueMap;

public class DeployJava extends WebComponent implements IHeaderContributor {

	private static final long serialVersionUID = 1L;
	private static final String ATTRIBUTE_WIDTH = "width";
	private static final String ATTRIBUTE_HEIGHT = "height";
	private static final String ATTRIBUTE_CODE = "code";
	private static final String ATTRIBUTE_CODEBASE = "codebase";
	private static final String ATTRIBUTE_ARCHIVE = "archive";
	private String minimalVersion;
	private IValueMap appletAttributes = new ValueMap();
	private IValueMap appletParameters = new ValueMap();

	public DeployJava(String id) {
		super(id);
	}

	public void setMinimalVersion(final String version) {
		this.minimalVersion = version;
	}

	public void setWidth(final Integer width) {
		appletAttributes.put(ATTRIBUTE_WIDTH, width);
	}

	public void setHeight(final Integer height) {
		appletAttributes.put(ATTRIBUTE_HEIGHT, height);
	}

	public void setCode(final String code) {
		appletAttributes.put(ATTRIBUTE_CODE, code);
	}

	public void setCodebase(final String codebase) {
		appletAttributes.put(ATTRIBUTE_CODEBASE, codebase);
	}

	public void setArchive(final String archive) {
		appletAttributes.put(ATTRIBUTE_ARCHIVE, archive);
	}

	public void addParameter(final String key, final Object value) {
		appletParameters.put(key, value);
	}

	@Override
	protected void onComponentTag(ComponentTag tag) {
		super.onComponentTag(tag);
		if ("applet".equalsIgnoreCase(tag.getName())) {
			final IValueMap tagAttributes = tag.getAttributes();
			final String wicketId = tagAttributes.getString("wicket:id");
			appletAttributes.putAll(tagAttributes);
			tagAttributes.clear();
			tagAttributes.put("wicket:id", wicketId);
		}
		tag.setName("script");
	}

	@Override
	public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
		final StringBuilder script = new StringBuilder();
		if (appletAttributes.size() > 0) {
			final JSONObject jsonAttributes = JSONObject.fromObject(appletAttributes);
			script.append("var attributes = " + jsonAttributes + ";");
		} else {
			script.append("var attributes = {};");
		}
		if (appletParameters.size() > 0) {
			final JSONObject jsonParameters = JSONObject.fromObject(appletParameters);
			script.append("var parameters = " + jsonParameters + ";");
		} else {
			script.append("var parameters = {};");
		}
		if (minimalVersion != null) {
			script.append("var version = \"" + minimalVersion + "\";");
		} else {
			script.append("var version = null;");
		}
		script.append("deployJava.runApplet(attributes, parameters, version);");
		replaceComponentTagBody(markupStream, openTag, script.toString());
	}

/*	public void renderHead(IHeaderResponse response) {
			response.renderJavaScriptReference(new PackageResourceReference(DeployJava.class,
				      "deployJava.js"));
	}*/
}
